﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace SHKF.B2C.YouZanYun.ServiceS.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        private readonly HttpClientYouZanYun clientYouZanYun;

        public TestController(HttpClientYouZanYun clientYouZanYun)
        {
            this.clientYouZanYun = clientYouZanYun;
        }
        public async Task Get()
        {
            StringBuilder htmlStringBuilder = new StringBuilder();
            htmlStringBuilder.Append("<html>");
            htmlStringBuilder.Append("<head><meta http-equiv=\"Content-Type\" content=\"charset=utf-8\" />");//支持中文
            htmlStringBuilder.Append("<style>");
            htmlStringBuilder.Append(".area { margin-left:16px; margin-top:16px; margin-bottom:20px;}");
            htmlStringBuilder.Append(".table { margin-left:16px; }");
            htmlStringBuilder.Append("td { border-bottom: 1px solid #808080;padding:8px }");
            htmlStringBuilder.Append("</style>");
            htmlStringBuilder.Append("<body>");
            htmlStringBuilder.Append("<ol style=\"font - size: 300 % \">");//让字体变大
            string datahtml = $"YouZanYun hello";
            List<string> list = new List<string>() { }; 
              
            list.Add(GetList());
            list.Add(GetCreate());
            list.Add(GetUpdate());
            list.Add(GetCreateMember()); //http://43.138.6.61:5003/Customer/CreateMember
            list.Add(GetPointsIncrease()); // "http://43.138.6.61:5003/Customer/PointsIncrease"
            list.Add(GetPointsDecrease()); //"http://43.138.6.61:5003/Customer/PointsDecrease"
            list.Add(GetPointsSync());     //"http://43.138.6.61:5003/Customer/PointsSync"

            
            list.ForEach(x =>
            {
                datahtml += "<li style='margin: 20px;color:blue;margin-top:32px'>" + x + "</li>";
            });

            htmlStringBuilder.Append(datahtml);
            htmlStringBuilder.Append("</ol>");
            htmlStringBuilder.Append("</body>");
            htmlStringBuilder.Append("</html>");
            string result = htmlStringBuilder.ToString();
            var data = Encoding.UTF8.GetBytes(result);

            Response.ContentType = "text/html";

            await Response.Body.WriteAsync(data, 0, data.Length);
        }

        private string GetList()
        {
            string html = "查询会员 Get: http://43.138.6.61:5003/Customer/list <br>";
             
            List<Row> rowsIn = new List<Row>();
            rowsIn.Add(new Row() { ColumnName = "page_no", ColumnType = "int", Remark = "序号从1开始" });
            rowsIn.Add(new Row() { ColumnName = "is_member", ColumnType = "int", Remark = "-1全部 0表示非会员，1表示会员" });
            rowsIn.Add(new Row() { ColumnName = "created_at_start", ColumnType = "DateTime", Remark = "创建会员开始时间" });
            rowsIn.Add(new Row() { ColumnName = "created_at_end", ColumnType = "DateTime", Remark = "创建会员结束时间" });
            string strRows = this.GetIn(rowsIn);

            List<Row> rowsOut = new List<Row>();
            rowsOut.Add(new Row() { ColumnName = "ScrmCustomerDto", ColumnType = "类", Remark = "&nbsp;" });

            strRows += this.GetOut(rowsOut);

            return html + strRows;
        }
        private string GetCreate()
        {
            string html = "创建客户 Post: http://43.138.6.61:5003/Customer/Create<br>";

            List<Row> rowsIn = new List<Row>();
            rowsIn.Add(new Row() { ColumnName = "CustomerCreate3Param", ColumnType = "类", Remark = "" }); 
            string strRows = this.GetIn(rowsIn);

            List<Row> rowsOut = new List<Row>();
            rowsOut.Add(new Row() { ColumnName = "CustomerCreate3Dto", ColumnType = "类", Remark = "&nbsp;" }); 
            strRows += this.GetOut(rowsOut);

            return html + strRows;
        }
        private string GetUpdate()
        {
            string html = "更新会员 Post: http://43.138.6.61:5003/Customer/Update <br>";

            List<Row> rowsIn = new List<Row>();
            rowsIn.Add(new Row() { ColumnName = "CustomerUpdate3Param", ColumnType = "类", Remark = "输入参数" });
            string strRows = this.GetIn(rowsIn);

            List<Row> rowsOut = new List<Row>();
            rowsOut.Add(new Row() { ColumnName = "CustomerUpdate3Dto", ColumnType = "类", Remark = "&nbsp;" });
            strRows += this.GetOut(rowsOut);

            return html + strRows;
        }
        private string GetCreateMember()
        {
            string html = "更新会员 Post: http://43.138.6.61:5003/Customer/CreateMember <br>";

            List<Row> rowsIn = new List<Row>();
            rowsIn.Add(new Row() { ColumnName = "CreateMemberParam", ColumnType = "类", Remark = "输入参数" });
            string strRows = this.GetIn(rowsIn);

            List<Row> rowsOut = new List<Row>();
            rowsOut.Add(new Row() { ColumnName = "CustomerUpdate3Dto", ColumnType = "类", Remark = "&nbsp;" });
            strRows += this.GetOut(rowsOut);

            return html + strRows;
        }

        private string GetPointsIncrease()
        {
            string html = "增加积分 Post: http://43.138.6.61:5003/Customer/PointsIncrease <br>";

            List<Row> rowsIn = new List<Row>();
            rowsIn.Add(new Row() { ColumnName = "Params", ColumnType = "类", Remark = "输入参数" });
            string strRows = this.GetIn(rowsIn);

            List<Row> rowsOut = new List<Row>();
            rowsOut.Add(new Row() { ColumnName = "CustomerPointsDto", ColumnType = "类", Remark = "&nbsp;" });
            strRows += this.GetOut(rowsOut);

            return html + strRows;
        }
        private string GetPointsDecrease()
        {
            string html = "减少积分 Post: http://43.138.6.61:5003/Customer/PointsDecrease <br>";

            List<Row> rowsIn = new List<Row>();
            rowsIn.Add(new Row() { ColumnName = "Params", ColumnType = "类", Remark = "输入参数" });
            string strRows = this.GetIn(rowsIn);

            List<Row> rowsOut = new List<Row>();
            rowsOut.Add(new Row() { ColumnName = "CustomerPointsDto", ColumnType = "类", Remark = "&nbsp;" });
            strRows += this.GetOut(rowsOut);

            return html + strRows;
        }
        private string GetPointsSync()
        {
            string html = "同步积分 Post: http://43.138.6.61:5003/Customer/PointsSync <br>";

            List<Row> rowsIn = new List<Row>();
            rowsIn.Add(new Row() { ColumnName = "CustomerPointsSyncParam", ColumnType = "类", Remark = "输入参数" });
            string strRows = this.GetIn(rowsIn);

            List<Row> rowsOut = new List<Row>();
            rowsOut.Add(new Row() { ColumnName = "CustomerPointsDto", ColumnType = "类", Remark = "&nbsp;" });
            strRows += this.GetOut(rowsOut);

            return html + strRows;
        }

        private string GetIn(List<Row> columns)
        {
            string html = "<div class=\"area\">入参:" +
                "<table class=\"table\" cellpadding=\"0\" cellspacing=\"0\">";
            html += this.GetColumnHeader();

            foreach(Row column in columns)
            {
                html += this.GetRow(column.ColumnName, column.ColumnType, column.Remark);
            }

            html += "</table></div>";
            return html;
        } 
        private string GetOut(List<Row> columns)
        {
            string html = "<div class=\"area\">出参:" +
                "<table class=\"table\" cellpadding=\"0\" cellspacing=\"0\">";
            html += this.GetColumnHeader();

            foreach (Row column in columns)
            {
                html += this.GetRow(column.ColumnName, column.ColumnType, column.Remark);
            }

            html += "</table></div>";
            return html;
        } 
        private string GetColumnHeader()
        {
            return $"<tr>" +
                $"<td class=\"header\">列名</td>" +
                $"<td class=\"header\">类型</td>" +
                $"<td class=\"header\">备注</td>" +
                $"</tr>";
        }
        private string GetRow(string columnName, string strType, string remark)
        {
            return $"<tr>" +
                $"<td>{columnName}</td>" +
                $"<td>{strType}</td>" +
                $"<td>{remark}</td>" +
                $"</tr>";
        }
    }
}
